#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
@file_name: multithreading_the_fibonacci_sequence.py
@date: 2025-07-21 17:09
@author: Falling Snow
@desc: 多线程获取Fibonacci序列
"""

import threading
import time


def print_fib(number: int) -> None:
    def fib(n: int) -> int:
        if n == 1:
            return 0
        elif n == 2:
            return 1
        else:
            return fib(n - 1) + fib(n - 2)

    print(f"fib({number}) 结果为: {fib(number)}")


def fibs_multithreading():
    """多线程获取fibonacci序列"""
    fortieth_thread = threading.Thread(target=print_fib, args=(40,))
    fortieth_thread.start()
    forty_one_thread = threading.Thread(target=print_fib, args=(41,))
    forty_one_thread.start()

    fortieth_thread.join()
    forty_one_thread.join()


if __name__ == '__main__':
    start = time.time()
    fibs_multithreading()
    fibs_multithreading()
    fibs_multithreading()

    end = time.time()

    print(f"获取数据完成, 共耗费{((end - start) / 3):.4f} second")
